package com.dgshanger.sanhxiaofeisc.ui.home.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.dgshanger.sanhxiaofeisc.R;

/**
 * Created by JacobHHH on 2018/4/27.
 */

public class NavigateDialog extends Dialog implements View.OnClickListener {

    private Context mContext;
    private TextView mTv_gd_map;
    private TextView mTv_bd_map;
    private TextView mTv_cancel;
    private TextView mTv_install;
    private View mV_gd_line;
    private View mV_bd_line;
    private String[] mPackNames=new String[]{"com.autonavi.minimap","com.baidu.BaiduMap"};

    public NavigateDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        mContext=context;
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        initView();
        checkItemInstall();
    }

    private void initView() {
        View view = View.inflate(mContext, R.layout.view_navigate_dialog, null);
        mTv_gd_map = view.findViewById(R.id.tv_gd_map);
        mTv_bd_map = view.findViewById(R.id.tv_bd_map);
        mTv_cancel = view.findViewById(R.id.tv_cancel);
        mTv_install = view.findViewById(R.id.tv_install);
        mV_gd_line = view.findViewById(R.id.v_gd_line);
        mV_bd_line = view.findViewById(R.id.v_bd_line);
        mTv_gd_map.setOnClickListener(this);
        mTv_bd_map.setOnClickListener(this);
        mTv_cancel.setOnClickListener(this);
        mTv_install.setOnClickListener(this);
        setContentView(view);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_gd_map://高德
                if (mSelectNavigateModelListener!=null){
                    mSelectNavigateModelListener.gaoDeMap();
                }
                break;
            case R.id.tv_bd_map://百度
                if (mSelectNavigateModelListener!=null){
                    mSelectNavigateModelListener.baiDuMap();
                }
                break;
            case R.id.tv_install://请安装高德或百度
                dismiss();
                break;
            case R.id.tv_cancel:
                dismiss();
                break;
        }
    }

    private void checkItemInstall() {

        for (int i = 0; i < mPackNames.length; i++) {
            boolean isInstall = OpenNavigationUtils.isInstallByread(mPackNames[i]);
            if (i==0){
                mTv_gd_map.setVisibility(isInstall?View.VISIBLE:View.GONE);
                mV_gd_line.setVisibility(isInstall?View.VISIBLE:View.GONE);
            }else {
                mTv_bd_map.setVisibility(isInstall?View.VISIBLE:View.GONE);
            }

            mTv_install.setVisibility(checkAllHaveInstall()?View.GONE:View.VISIBLE);
        }
    }

    private boolean checkAllHaveInstall(){

        for (int i = 0; i < mPackNames.length; i++) {
            if (OpenNavigationUtils.isInstallByread(mPackNames[i])){
                return true;
            }
        }
        return false;
    }

    @Override
    public void show() {
        if (!((Activity)mContext).isFinishing()){
            super.show();
        }
    }

    private OnSelectNavigateModelListener mSelectNavigateModelListener;

    public interface OnSelectNavigateModelListener {

        void gaoDeMap();

        void baiDuMap();
    }

    public void setSelectNavigateModelListener(OnSelectNavigateModelListener listener){
        mSelectNavigateModelListener=listener;
    }

}
